#include "head.h"

// 子线程的入口
void * pthread_fun(void *p){
    pool_t *pool = (pool_t *)p;

    pthread_mutex_lock(&pool->pthread_lock);
    // 去队列中拿任务
    if (pool->queue.size <= 0){
        pthread_cond_wait(&pool->cond, &pool->pthread_lock);
    }
    int net_fd = pool->queue.head->net_fd;
    // 删除
    de_queue(&pool->queue);
    pthread_mutex_unlock(&pool->pthread_lock);

    // 读取用户名
    char uname[20] = {0};
    //客户端连续三次输入错误密码的标记
    int flag_break = 0;
    //判断客户端能否登录
    login_check(net_fd, &flag_break, uname);
    //标记为1，说明客户端断开连接，关闭客户端的连接，继续下次监听
    if(flag_break == 1){
        printf("关闭客户端连接\n");
        close(net_fd);
        return NULL;
    }

    MYSQL *conn;
    //MYSQL_RES *result;
    //MYSQL_ROW row;

    conn = mysql_init(NULL);
    if (!mysql_real_connect(conn, "localhost", "root", "zs123456", "student", 0, NULL, 0)) {
        printf( "%s\n", mysql_error(conn));
        exit(1);
    }
    mysql_set_character_set(conn, "utf8mb4");
    //初始路径
    char cur_path[100]={"/"};
    char last_deleted_file[1024] = "";
    char last_deleted_path[1024] = "";
    while(1){
        // 接收命令 
        char buf_cmd[100] = {0};
        int res_recv = recv(net_fd, buf_cmd, sizeof(buf_cmd), 0);   
        ERROR_CHECK(res_recv, -1, "recv");

        if (strcmp(buf_cmd, "") != 0){

            printf("client cmd: %s\n", buf_cmd);
            // 将命令头转换为枚举类型
            char *cmd_head;
            char *cmd_body;
            COMMAND cmd;
            if (strcmp(buf_cmd, "ls\n") == 0) {
                cmd = ls;
            }else if (strcmp(buf_cmd, "pwd\n") == 0) {
                cmd = pwd;

            }else if(strcmp(buf_cmd, "undorm\n") == 0){
                cmd = undo_rm;
            }
            else {
                // 分析命令: 切割命令，获取命令头，命令体
                cmd_head = strtok(buf_cmd, " ");
                printf("cmd head : %s\n", cmd_head);
                if (strcmp(cmd_head, "upload") == 0) {
                    cmd = upload;
                    cmd_body = strtok(NULL, "\n");
                    printf("cmd body : %s\n", cmd_body);
                } else if (strcmp(cmd_head, "download") == 0) {
                    cmd = download;
                    cmd_body = strtok(NULL, "\n");
                    printf("cmd body : %s\n", cmd_body);
                } else if (strcmp(cmd_head, "cd") == 0) {
                    cmd = cd;
                    cmd_body = strtok(NULL,  " ");
                    printf("cmd body : %s\n", cmd_body);
                } else if (strcmp(cmd_head, "mkdir") == 0) {
                    cmd = mk_dir;
                    cmd_body = strtok(NULL, "\n");
                    printf("cmd body : %s\n", cmd_body);
                } else if (strcmp(cmd_head, "rm") == 0) {
                    cmd = rm_dir;
                    cmd_body = strtok(NULL,  "\n");
                    printf("cmd body : %s\n", cmd_body);
                } // 无需判错，客户端已做相应处理
            }

            // 根据命令调用相应函数
            switch (cmd){
            case upload: recv_bigfile(net_fd, cmd_body, uname, conn); break;
            case download: send_bigfile(net_fd, cmd_body, uname, conn); break;
            case ls: client_ls(conn, net_fd, uname, cur_path); break;
                     //case ll: client_check_order(stack, net_fd, "ll"); break;
            case pwd: send(net_fd, cur_path, strlen(cur_path), MSG_NOSIGNAL); break;
            case mk_dir:client_mkdir(cur_path, net_fd, cmd_body, conn, uname); break;
            case rm_dir: client_rm(conn, net_fd,  cur_path,cmd_body, last_deleted_file, last_deleted_path); break;
            case cd: client_cd( net_fd,conn, cmd_body,cur_path); break;
            case undo_rm:client_undo_rm(conn, net_fd, last_deleted_file, last_deleted_path);break;
            }

            printf("operate finished\n");
            //close(net_fd);
        }else {
            continue;
        }
    }

    //mysql_free_result(result);
    mysql_close(conn);

    return NULL;
}
